package com.cardpay.supplier;

import com.cardpay.common.log.Log;
import com.cardpay.common.util.ConfigUtil;
import com.cardpay.common.util.SQLHelper;
import com.cardpay.page.PageBean;
import org.slf4j.Logger;

import java.util.List;
import java.util.RandomAccess;
import java.util.UUID;

/**
 * Created by asusa on 2017/1/23.
 */
public class SupplierService {

    private static final Logger log = Log.get();

    private SupplierDao supplierDao=new SupplierDao();

    /**
     * 查询供应商
     *
     * @param id
     *            供应商编号
     * @return
     */
    public Supplier find(String id) {
        Supplier supplier = supplierDao.find(id);

        return supplier;
    }

    public List<Supplier> findAll(){
        return supplierDao.list();
    }

    /**
     * 根据账单编号查询对应的供应商
     *
     * @param billId
     *            用户编号
     * @return 角色实体对象
     */
    public Supplier findByBillId(String billId) {
        Supplier supplier = supplierDao.findByBillId(billId);

        return supplier;
    }

    public PageBean<Supplier> findPagingSuppliers(int pageNum, String name) {

        int pageSize = ConfigUtil.getPageSize();

        // 生成SQL语句的工具类（此处接受一些设置）
//        SQLHelper sqlHelper = new SQLHelper("t_supplier", "s") //
//                .addWhere(null != name && 0 < name.length(), "s.name LIKE ?", "%" + name + "%") //

         SQLHelper sqlHelper = new SQLHelper("t_supplier") //
         .addWhere(null != name && 0 < name.length(), "name LIKE ?", "%" + name + "%"); //

        // 查询指定页的数据列表
        PageBean<Supplier> pageBeans = supplierDao.findPagingSupplier(pageNum, pageSize, sqlHelper);

        List<Supplier> suppliers = pageBeans.getRecordList();

        return pageBeans;
    }

    public boolean add(Supplier supplier) {

        String uuid = UUID.randomUUID().toString(); // 生成UUID主键

        log.debug("primary key(uuid): {}", uuid);

        supplier.setId(uuid); // 设置主键

        if (0 < supplierDao.add(supplier)) {
            return true;
        }

        return false;
    }

    public boolean delete(String id) {
        if (0 < supplierDao.delete(id)) {
            return true;
        }
        return false;
    }

    public boolean update(Supplier supplier) {
        if (0 < supplierDao.update(supplier)) {
            return true;
        }
        return false;
    }
}
